package com.magiccompass.alipay.barcode.util;

import java.net.URI;
import java.util.TreeMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.client.RestTemplate;

import com.magiccompass.alipay.barcode.constants.AlipayPartnerConstants;
import com.magiccompass.alipay.barcode.controller.AlipayBarcodeCancelController;
import com.magiccompass.alipay.barcode.entity.AlipayBarcodeQueryRequest;
import com.magiccompass.alipay.barcode.entity.MerchantTransaction;
import com.magiccompass.alipay.barcode.exception.StandardException;
import com.magiccompass.alipay.barcode.responsehandler.QueryResponseHandler;
import com.magiccompass.alipay.barcode.security.model.LoginUserModel;
import com.magiccompass.alipay.barcode.service.AlipayBarcodeService;
import com.magiccompass.alipay.barcode.service.MerchantBusinessService;

public class AlipayTransactionQueryUtil {
	static Logger logger = LogManager.getLogger(AlipayBarcodeCancelController.class.getName());
	public static String queryTransactionStatus(Integer mtId, MerchantBusinessService merchantBusinessService,AlipayBarcodeService alipayBarcodeService) throws StandardException{
		String returnValue = "";
		MerchantTransaction mt =  merchantBusinessService.findMerchantTransactionById(mtId);
		LoginUserModel currentUser = UserUtil.getCurrentLoginUser();
		AlipayBarcodeQueryRequest abrq = AlipayBarcodeRequestUtil.alipayBarcodeQueryRequestEntityInit(mt,currentUser.getLoginUserId());
		
		TreeMap<String,String> barcodeRequestParameter4AlipayTreeMap = AlipayBarcodeRequestUtil.makeParameterMap4Alipay(abrq, alipayBarcodeService);
		String sign = AlipayBarcodeRequestUtil.getSign(barcodeRequestParameter4AlipayTreeMap);
		abrq.setSign(sign);
		
		alipayBarcodeService.save(abrq);

		barcodeRequestParameter4AlipayTreeMap.put("sign", sign);
		barcodeRequestParameter4AlipayTreeMap.put("sign_type", AlipayPartnerConstants.SIGN_TYPE);
		
		if(abrq.getId() != null){ // success insert into DB
			try {							
				RestTemplate restTemplate = new RestTemplate();
				String alipayBarcodeServerURL = AlipayPartnerConstants.ALIPAY_GATEWAY;
		    	String alipayBarcodeParameter = UrlUtil.makeParameterMap2String(barcodeRequestParameter4AlipayTreeMap);
		    	String restURL = alipayBarcodeServerURL+"?"+alipayBarcodeParameter;
		    	logger.info("restURL:"+restURL);
		    	URI uri = new URI(restURL);
		    	//String response = restTemplate.getForObject(restURL, String.class);
		    	String response = restTemplate.getForObject(uri, String.class);
		    	//String response = QueryResponseHandler.RESULT_QUERY_FAILEDFAIED_4_TEST;
		    	logger.info("response is:"+response);				
		    	returnValue = QueryResponseHandler.handleResponse(response, abrq, alipayBarcodeService);		    	
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}else{
    		StandardException se = new StandardException("AlipayBarcodeQueryRequest haven't insert into DB.");
    		throw se;
    	}
		
		return returnValue;
	}
}
